package com.nowcoder.community.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.nowcoder.community.contant.Role;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

@Data
public class User {
    private int id;
    private String username;
    private String password;
    private String salt;
    private String email;
    private int type;
    private int status;
    private String activationCode;
    private String headerUrl;
    private Date createTime;

    /**
     * 获取用户权限
     * */
    @JsonIgnore // redis 序列化时忽略 authority 属性
    public Collection<? extends GrantedAuthority> getAuthorities(){
        SimpleGrantedAuthority authority = null;
        if (type == 0){
            authority = new SimpleGrantedAuthority(Role.USER);
        }else if (type == 1){
            authority = new SimpleGrantedAuthority(Role.ADMIN);
        }else if(type == 2){
            authority = new SimpleGrantedAuthority(Role.MODERATOR);
        }
        return Arrays.asList(authority);
    }
}
